package com.lzh.dlykserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TTran;
import com.lzh.dlykserver.entity.TTranRemark;
import com.lzh.dlykserver.mapper.TTranMapper;
import com.lzh.dlykserver.mapper.TTranRemarkMapper;
import com.lzh.dlykserver.query.TranDTO;
import com.lzh.dlykserver.query.TransactionQuery;
import com.lzh.dlykserver.result.TransactionExcel;
import com.lzh.dlykserver.service.TransactionService;
import com.lzh.dlykserver.util.JWTUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TransactionServiceImpl implements TransactionService {

    private final TTranMapper tTranMapper;
    private final TTranRemarkMapper tTranRemarkMapper;

    @Override
    public PageInfo<TTran> getTranByPage(Integer current, TransactionQuery tranQuery) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TTran> list = tTranMapper.selectTranByPage(tranQuery);
        //封装分页数据到PageInfo
        PageInfo<TTran> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public TTran getTransactionById(Integer id) {
        return tTranMapper.getTransactionById(id);
    }

    @Override
    public int deleteTransaction(Integer id) {
        return tTranMapper.deleteTransaction(id);
    }

    @Override
    public int batchDeleteTransaction(List<Integer> idList) {
        return tTranMapper.batchDeleteTransaction(idList);
    }

    @Override
    public int saveTransactionRemark(TransactionQuery transactionQuery) {
        TTranRemark tTranRemark = new TTranRemark();
        BeanUtils.copyProperties(transactionQuery, tTranRemark);
        Integer id = JWTUtils.parseUserFromJWT(transactionQuery.getToken()).getId();
        tTranRemark.setCreateBy(id);
        tTranRemark.setCreateTime(new Date());
        return tTranRemarkMapper.insertSelective(tTranRemark);
    }

    @Override
    public int updateTransaction(TranDTO transactionQuery) {
        TTran tTran = new TTran();
        BeanUtils.copyProperties(transactionQuery, tTran);

        Integer id = JWTUtils.parseUserFromJWT(transactionQuery.getToken()).getId();
        tTran.setEditBy(id);
        tTran.setEditTime(new Date());
        return tTranMapper.updateByPrimaryKeySelective(tTran);
    }

    @Override
    public List<TransactionExcel> getTransactionByExcel(List<String> idList) {
        List<TransactionExcel> transactionExcelList = new ArrayList<>();

        List<TTran> tTransactionList = tTranMapper.selectTransactionByExcel(idList);

        // 将TTransaction对象转换为TransactionExcel对象
        for (TTran tTransaction : tTransactionList) {
            TransactionExcel transactionExcel = new TransactionExcel();
            transactionExcel.setTranNo(tTransaction.getTranNo());
            transactionExcel.setMoney(tTransaction.getMoney());
            transactionExcel.setProductName(tTransaction.getProductDo().getName());
            transactionExcel.setExpectedDate(tTransaction.getExpectedDate());
            transactionExcel.setStage(tTransaction.getStageDO().getTypeValue());
            transactionExcel.setCreateTime(tTransaction.getCreateTime());
            // 设置其他字段...
            transactionExcelList.add(transactionExcel);
        }
        return transactionExcelList;
    }


}
